const electron = require('electron');
const fs = require('fs');
const iconv = require('iconv-lite');
const { app, BrowserWindow, ipcMain } = electron;
const { READ_FILE } = require('./src/ipcTypes')

let mainWindow;

app.on('ready', () => {
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 500,
    webPreferences: {
      webSecurity: false,
    }
  });
  // mainWindow.loadURL(`file://${__dirname}/index.html`);
  mainWindow.loadURL('http://47.100.185.211:8885'); // 线上环境
  // mainWindow.loadURL('http://localhost:8885'); // 本地dist环境
  // mainWindow.loadURL('http://localhost/client'); // 无效本地dist环境 http://localhost:8885
  // mainWindow.loadURL('http://localhost:8003'); // 本地dev环境
});

ipcMain.on(READ_FILE, (event, path, encode, readFileActionId) => {
  fs.readFile(path, function(err, data){
    if (err) {
      mainWindow.webContents.send(readFileActionId, false, err);
    } else {
      // mainWindow.webContents.send(readFileActionId, true, data);
      const str = iconv.decode(data, encode);
      mainWindow.webContents.send(readFileActionId, true, str);
    }
  });
});
